<?php
namespace Dashboard\Controller;
use  Dashboard\Model\AdminModel;
class AdminController extends BaseController{
    static public $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new AdminModel();
    }
    public function index(){
        $a        = (int)(I("currpage"));//跳转页数
        $sou      = I('sou');
        $pagesize = 10;
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        if($sou){
            $content = I('content');
            $totalpage = 1;
            $currpage  = 1;
            if($sou == 1){
                $list      = sels("admin","","name like '%{$content}%'","id desc");
            }
            if($sou == 2){
                $list      = sels("admin","","numbers like '%{$content}%'","id desc");
            }
        }else{
            $result = currpage("admin","",$pagesize,$currpage,"id desc");
            $totalpage = $result['totalpage'];
            $list      = $result['list'];
            if($result['currpage']){
                $currpage = $result['currpage'];
            }
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function form(){
        $id = I('id');
        if($id){
            $list = sel("admin","","id = {$id}");
            $this->assign("list",$list);
        }
        $this->display();
    }
    public function info(){
        $list = sel("admin"," * ","id = {$_SESSION['admin']['id']}");
        $this->assign("list",$list)->assign("que",1);
        $this->display();
    }
    public function add(){
        $que               = I('que');
        if($que){
            $data['password'] = md5(I('xin'));
        }else{
            $ps                = I('ps');
            if($ps == I('password')){
                $data['password'] = $ps;
            }else{
                $data['password'] = md5(I('password'));
            }
             $data['numbers']  = I('numbers'); $data['type'] = I('type');
            if(I('status')){
                $data['status'] = I('status');
            }else{
                $data['status'] = 1;
            }
        }
        $data['gong']     = I('gong');
        $data['type']     = I('type');
        $data['store_id'] = I('store_id');
        $data['name']      = I('name');
        $data['email']      = I('email');
        $data['phone']      = I('phone');
        $id                 = I('id');
            if($id == null){
                $data['create_at'] = time();
                //处理图片
                if($_FILES['one']['name'] != null){
                    $info                   = $this->imgUpload("admin/");
                    $savename               = $info['one']['savename'];
                    $savepath               = $info['one']['savepath'];
                    $data['img']            = "Data/".$savepath.$savename;
                }
                $result = ins("admin",$data);
                if($result){
                    $content = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time())."添加了管理员：‘".$data['name']."’的信息";
                    $log     = ins("logs",array("content"=>$content,"admin_id"=>$_SESSION['admin']['id'],"create_at"=>time()));
                    $this->success("添加成功",U('form'));
                }
            }else{
                $data['up_at']     = time();
                $img                = I('img');
                //处理图片
                if($_FILES['one']['name'] != null){
                    unlink($img);
                    $info                   = $this->imgUpload("admin/");
                    $savename               = $info['one']['savename'];
                    $savepath               = $info['one']['savepath'];
                    $data['img']            = "Data/".$savepath.$savename;
                }
                $result = up_one("admin",array("id"=>$id),$data);
                if($result){
                    $content = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time())."修改了管理员：‘".$data['name']."’的信息";
                    $log     = ins("logs",array("content"=>$content,"admin_id"=>$_SESSION['admin']['id'],"create_at"=>time()));
                   if(QUAN == 3){
                       $this->success("修改成功",U('info'));
                   }else{
                       $this->success("修改成功",U('index'));
                   }
                }else{
                    $this->success("没有信息变更，修改失败");
                }
            }
    }
    public function checks(){
        $che = I('che');
        if($che>0){
            $a   = $this->model->create(array("password"=>$_POST['password'],"name"=>$_POST['name']));
        }else{
            $a   = $this->model->create();
        }
        if(!$a){
            $e = $this->model->getError();
            $this->ajaxReturn(array("status"=>1,"info"=>$e));
        }else{
            $this->ajaxReturn(array("status"=>2));
        }
    }
    public function password(){
        $password = md5(I("password"));
        $result   = sel("admin","","id = {$_SESSION['admin']['id']} and password = '{$password}'");
        if($result){
            $this->ajaxReturn(array("status"=>1));
        }else{
            $this->ajaxReturn(array("status"=>2));
        }
    }
}